<?php

namespace App\Http\Controllers\Backend;

use App\Models\AdminRole;
use App\Models\AdminUser;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserController extends BaseController
{
    //管理员列表
    public function index()
    {
        $key = \request('key');
        $adminusers = AdminUser::with('roles')->when($key, function ($query) use ($key) {
                          return $query->where('username','like', '%'.$key .'%');
                      })->paginate(10);
        return view('backend.user.index', compact('adminusers','key'));
    }

    //管理员添加页面
    public function create()
    {
        $roles=AdminRole::all();
        return view('backend.user.add',compact('roles'));
    }
    //分配角色
    public function storeRole()
    {
        
    }

    //管理员添加行为
    public function store()
    {
        //验证
        $this->validate(\request(),[
            'username' => 'required|alpha|unique:admin_users|min:5|max:10',
            'password' => 'required|min:6|max:12|confirmed',
            'password_confirmation' => 'required|min:6|max:12',
            'rolename' => 'required|integer'
        ]);
        //逻辑
        $param=[
            'username' => \request('username'),
            'password' => bcrypt(\request('password'))
        ];
        if($adminuser=AdminUser::create($param)){
            $adminuser->roles()->attach(\request('rolename'));
        }

        //渲染
        return redirect(admin_prefix().'/users');
    }

    //编辑页面
    public function edit(AdminUser $adminUser)
    {
        $roles=AdminRole::all();
        $myrole= $adminUser->roles;
        return view('backend.user.edit',compact('adminUser','roles','myrole'));
    }
    //编辑行为
    public function update(AdminUser $adminUser)
    {
        //验证
        $this->validate(\request(),[
            'username' => 'required|alpha|min:5|max:10|unique:admin_users,username,'.$adminUser->id,
//            'password' => 'min:6|max:12',
//            'password_confirmation' => 'min:6|max:12',
            'rolename' => 'required|integer'
        ]);
        //逻辑
        $adminUser->username = \request('username');
        if(!empty(\request('password') && !empty('password_confirmation'))) $adminUser->password = bcrypt(\request('password'));

        if($adminUser->save()){
            if(!$adminUser->roles->isEmpty()){
                $adminUser->roles()->detach($adminUser->roles->first()->id);
            }
            $adminUser->roles()->attach(\request('rolename'));
        }


        //渲染
        return redirect(admin_prefix().'/users');
    }

    //删除行为
    public function destroy()
    {

        $ids=is_array(\request('id'))?\request('id'):[\request('id')];
        DB::transaction(function () use ($ids) {
            //删除关联关系
            foreach ($ids as $id){
                $adminuser=AdminUser::find($id);
                $adminuser->deleteRole($adminuser->roles->first()->id);
            }
            //删除用户
            AdminUser::whereIn('id',$ids)->delete();

        });


        return ['status' => '1', 'code' => 200];
    }
}
